package org.insia.smartposse.dao;

import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IFreelanceDAO;
import org.insia.smartposse.metier.Freelance;
import org.insia.smartposse.metier.webservice.LightFreelance;
import org.insia.smartposse.utils.PasswordEncoder;

/**
 * DAO for Freelance class based on GenericDAO.
 * @author sok hout
 *
 */
public class FreelanceDAO 
extends GenericDAO<Freelance, Long>
implements IFreelanceDAO {
	
	/**
	 * Empty constructor.
	 *
	 */
	public FreelanceDAO() {
		type = Freelance.class;
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceDAO#createLightFreelance(org.insia.smartposse.metier.webservice.LightFreelance)
	 */
	public LightFreelance createLightFreelance(LightFreelance freelance) {
		try {
			Freelance free = new Freelance();
			free.setDescription(freelance.getDescription());
			free.setEmail(freelance.getEmail());
			free.setFee(freelance.getFee());
			free.setFirstName(freelance.getFirstName());
			free.setGender(freelance.getGender());
			free.setLastName(freelance.getLastName());
			free.setLogin(freelance.getLogin());
			free.setPassword(PasswordEncoder.encode(freelance.getPassword()));
			
			if (freelance.getSkillSet().size() != 0) {
				freelance.setSkillSet(DAOFactory.getFreelanceSkillDAO().createLightFreelanceSkill(freelance.getSkillSet()));
			}
			
			if (freelance.getAlertSet().size() != 0) {
				freelance.setAlertSet(DAOFactory.getFreelanceAlertDAO().createLightFreelanceAlert(freelance.getAlertSet()));
			}
			
			if (freelance.getPortfolioSet().size() != 0) {
				freelance.setPortfolioSet(DAOFactory.getFreelancePortfolioDAO().createLightFreelancePortfolio(freelance.getPortfolioSet()));
			}
			
			if (freelance.getProjectSet().size() != 0) {
				freelance.setProjectSet(DAOFactory.getProjectDAO().createLightProject(freelance.getProjectSet()));
			}
			
		
			freelance.setId(DAOFactory.getFreelanceDAO().create(free));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return freelance;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceDAO#updateLightFreelance(org.insia.smartposse.metier.webservice.LightFreelance)
	 */
	public LightFreelance updateLightFreelance(LightFreelance freelance) {
		try {
			Freelance free = DAOFactory.getFreelanceDAO().get(freelance.getId());
			free.setDescription(freelance.getDescription());
			free.setEmail(freelance.getEmail());
			free.setFee(freelance.getFee());
			free.setFirstName(freelance.getFirstName());
			free.setGender(freelance.getGender());
			free.setLastName(freelance.getLastName());
			free.setLogin(freelance.getLogin());
			
			if (freelance.getSkillSet().size() != 0) {
				freelance.setSkillSet(DAOFactory.getFreelanceSkillDAO().updateLightFreelanceSkill(freelance.getSkillSet()));
			}
			
			if (freelance.getAlertSet().size() != 0) {
				freelance.setAlertSet(DAOFactory.getFreelanceAlertDAO().updateLightFreelanceAlert(freelance.getAlertSet()));
			}
			
			if (freelance.getPortfolioSet().size() != 0) {
				freelance.setPortfolioSet(DAOFactory.getFreelancePortfolioDAO().updateLightFreelancePortfolio(freelance.getPortfolioSet()));
			}
			
			if (freelance.getProjectSet().size() != 0) {
				freelance.setProjectSet(DAOFactory.getProjectDAO().updateLightProject(freelance.getProjectSet()));
			}
			
		
			DAOFactory.getFreelanceDAO().update(free);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return freelance;
	}
}
